class StoreStaff
  module StaffSalary
    extend ActiveSupport::Concern

    def self.included(base)
    end

    DEFAULT_SALARY_MONTH = Time.now.last_month

    def current_salary_month
      Time.now.last_month.strftime("%Y-%m")
    end

    def attendence_amount(month = DEFAULT_SALARY_MONTH)
      attendences.by_month(month).without_off.sum(&:amount)
    end

    def reward_amount(month = DEFAULT_SALARY_MONTH)
      rewards.by_month(month).sum(&:amount)
    end

    def discipline_amount(month = DEFAULT_SALARY_MONTH)
      disciplines.by_month(month).sum(&:amount)
    end

    def overtime_hours
      overtimes.overtime_hours.sum(&:hour)
    end

    def overtime_amount(month = DEFAULT_SALARY_MONTH)
      overtimes.overtime_amount.by_month(month).sum(&:amount)
    end

    def attendence_off_hours
      attendences.attendence_off.sum(&:hour)
    end

    def reset_salary(new_salary)
      self.update!(basic_salary: new_salary)
    end

    def should_pay_amount(month = DEFAULT_SALARY_MONTH)
      basic_salary.to_f + subsidy_amount.to_f + reward_amount.to_f + overtime_amount(month).to_f + commission_amount.to_f
    end

    def cutfee_amount(month = DEFAULT_SALARY_MONTH)
      insurance_amount.to_f + should_cutfee(month).to_f
    end

    def should_cutfee(month = DEFAULT_SALARY_MONTH)
      discipline_amount(month) + attendence_amount(month)
    end

    def commission_amount(month = DEFAULT_SALARY_MONTH)
      commission.to_f * store.orders.amount_by_month(month)
    end

    def actual_payment
      should_pay_amount.to_f - cutfee_amount.to_f
    end

    def has_salary?
      salary.present?
    end

    def salary
      self.salaries.last_month_salary.last
    end

    def unchecked_salary
      self.salaries.uncheck.last_month_salary.last
    end

    def checked_salary
      self.salaries.checked.last_month_salary.last
    end

    def review_salary
      @salary = salary
      if @salary.present?
        return @salary if @salary.salary_status.present?
        self.reset_salary(@salary.basic) if self.basic_salary != @salary.basic
        return @salary = self unless @salary.salary_status.present?
      else
        initialize_salary
      end
    end

    def debit_total
      should_cutfee
    end

    def reward_total
      reward_amount.to_f + overtime_amount.to_f
    end

    def actual_amount
      actual_payment.round(2)
    end

    def salary_status
      salary.status
    end

    def salary_id
      salary.id
    end

    def initialize_salary(created_month = nil, basic = nil)
      @salary = self.salaries.create!(
        store_id: store_id,
        basic: basic || basic_salary,
        subsidy: {
          gangwei: gangwei,
          canfei: canfei,
          zhusu: zhusu,
          laobao: laobao,
          jiaotong: jiaotong,
          gaowen: gaowen,
          other: other_subsidy
        },
        subsidy_amount: subsidy_amount,
        insurance: {
          yiliao: yiliao,
          yanglao: yanglao,
          shiye: shiye,
          gongjijin: gongjijin
        },
        insurance_amount: insurance_amount,
        commission: commission,
        commission_amount: commission_amount,
        created_month: created_month || Time.now.last_month.strftime("%Y%m"),
        attendence_amount: attendence_amount,
        discipline_amount: discipline_amount,
        reward_amount: reward_amount,
        overtime_amount: overtime_amount,
        debit_total: debit_total,
        reward_total: reward_total,
        actual_amount: actual_amount
      )
    end

    def subsidy
      read_attribute(:subsidy) || {}
    end

    def subsidy_by(name)
      subsidy[name]
    end

    #岗位
    def gangwei
      subsidy["gangwei"].to_f
    end

    #住宿
    def zhusu
      subsidy["zhusu"].to_f
    end

    #餐补
    def canfei
      subsidy["canfei"].to_f
    end

    # 劳保
    def laobao
      subsidy["laobao"].to_f
    end

    #交通
    def jiaotong
      subsidy["jiaotong"].to_f
    end

    #高温
    def gaowen
      subsidy["gaowen"].to_f
    end

    def other_subsidy
      subsidy["other"].to_f
    end

    def subsidy_amount
      (gangwei + zhusu + canfei + laobao + jiaotong + gaowen + other_subsidy).round(2) || 0.0
    end

    def insurance
      read_attribute(:insurance) || {}
    end

    def yiliao
      insurance["yiliao"].to_f
    end

    def yanglao
      insurance["yanglao"].to_f
    end

    def shiye
      insurance["shiye"].to_f
    end

    def gongjijin
      insurance['gongjijin'].to_f
    end

    def insurance_amount
      (yiliao + yanglao + shiye + gongjijin).round(2) ||  0.0
    end

  end
end
